package com.example.mychat;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;

import com.example.mychat.Bean.PyqItem;

import java.util.Date;

public class PostActivity extends AppCompatActivity {
    private EditText pyqedit;
    private RadioGroup chaooseimg;
    private Button pyqbtn;
    int contentimg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_post);
        initView();
        setListener();
    }

    private void setListener() {
        chaooseimg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.rediobtn1:
                        contentimg=R.mipmap.b;
                        break;
                    case R.id.rediobtn2:
                        contentimg=R.mipmap.a;
                        break;
                    case R.id.rediobtn3:
                        contentimg=R.mipmap.c;
                        break;
                }
            }
        });
        pyqbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (contentimg!=0){
                    SharedPreferences sp=getSharedPreferences
                            ("userinfo",MODE_PRIVATE);
                    PyqItem p=new PyqItem();
                    p.setId(0);
                    p.setUserimg(sp.getInt("myimg",-1));
                    p.setUsername(sp.getString("myname",""));
                    p.setPyqcontent(pyqedit.getText().toString());
                    p.setPyqimg(contentimg);
                    p.setDeta(new Date());
                    if (p.save()){
                        Intent intent=new Intent(PostActivity.this,PyqActivity.class);
                        startActivity(intent);
                        finish();
                    }
                }
            }
        });
    }

    private void initView() {
        pyqedit=findViewById(R.id.pyqedit);
        pyqbtn=findViewById(R.id.pyqbtn);
        chaooseimg=findViewById(R.id.chaooseimg);
    }
}
